package com.balance.anole.erp.common.dialog;

import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.balance.anole.erp.common.model.ProductItemVO;
import com.balance.anole.erp.common.provider.ProductSelectorContentProvider;
import com.balance.anole.erp.common.provider.ProductSelectorLabelProvider;
import com.balance.anole.erp.util.Messages;

public class ProductSelectorSearchResult extends Composite {

	private Table table;
	private TableViewer tableViewer = null;

	private ProductItemVO selectedProduct;

	private WritableValue wv = new WritableValue(null, ProductItemVO.class);

	/**
	 * Create the composite
	 * @param parent
	 * @param style
	 */
	public ProductSelectorSearchResult(Composite parent, int style) {
		super(parent, style);
		this.setLayout(new FormLayout());
		tableViewer = new TableViewer(this, SWT.BORDER|SWT.FULL_SELECTION);
		createColumns(tableViewer, new String[]{Messages.ProductSelector_Column_Prod_Sn, Messages.ProductSelector_Column_Prod_Name, Messages.ProductSelector_Column_Color, Messages.ProductSelector_Column_Size}, new int[] {100, 200, 100, 100});

		table = tableViewer.getTable();
		final FormData fd_table = new FormData();
		fd_table.top = new FormAttachment(0, 10);
		fd_table.bottom = new FormAttachment(100, -5);
		fd_table.right = new FormAttachment(100, -5);
		fd_table.left = new FormAttachment(0, 5);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		tableViewer.setContentProvider(new ProductSelectorContentProvider());
		tableViewer.setLabelProvider(new ProductSelectorLabelProvider());

		tableViewer.addDoubleClickListener(new IDoubleClickListener() {

			public void doubleClick(DoubleClickEvent event) {
				StructuredSelection selection = (StructuredSelection)event.getSelection();
				Object element = selection.getFirstElement();
				if(element != null){
					selectedProduct = (ProductItemVO)element;
					wv.doSetValue(selectedProduct);
				}
			}
		});
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	protected void createColumns(final TableViewer viewer, String[] titles, int[] bounds) {
		for (int i = 0; i < titles.length; i++) {
			TableViewerColumn viewerColumn  = new TableViewerColumn(viewer, SWT.NONE);
			final TableColumn column = viewerColumn.getColumn();
			column.setText(titles[i]);
			column.setWidth(bounds[i]);
			column.setResizable(true);
			column.setMoveable(true);
		}

	}
	public TableViewer getTableViewer() {
		return tableViewer;
	}

	public ProductItemVO getSelectedProduct(){
		return selectedProduct;
	}

	public void addProductSelectListener(IChangeListener listener){
		wv.addChangeListener(listener);
	}
}
